/// paragraph的特征
/// 1. 字号大号
/// 2. 字体普通

import 'package:extended_text/extended_text.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:cunyutech_flutter_ui/src/theme/main.dart';

class CYParagraph extends StatelessWidget {
  final double lineHeight;
  final double height;
  final double width;
  final double fontSize;
  final FontWeight fontWeight;
  final Color color;
  final String data;
  final EdgeInsetsGeometry margin;
  final EdgeInsetsGeometry padding;
  final TextOverflow overflow;
  final Color backgroundColor;
  final AlignmentGeometry alignment;
  final TextAlign textAlign;
  final int maxLines;
  /// 是否可以选中，为true时不可以设置overfolow
  final bool selectable;
  /// 是否可以展开，为true时不可以设置overfolow，需要设置maxLines和width
  final bool expandable;
  /// 展开事件
  final VoidCallback onExpanded;
  /// 圆角
  final double borderRadius;

  final List<Shadow> shadows;
  /// 点击事件
  final VoidCallback onTap;

  const CYParagraph(this.data, {
    Key key, 
    this.lineHeight = CYLineHeight.Default, 
    this.fontSize = CYFontSize.Default, 
    this.fontWeight, 
    this.color, 
    this.overflow,
    this.margin, 
    this.padding,
    this.backgroundColor = Colors.transparent,
    this.height,
    this.alignment,
    this.width,
    this.textAlign = TextAlign.left,
    this.maxLines,
    this.selectable = false,
    this.shadows,
    this.expandable = false,
    this.onExpanded,
    this.borderRadius = 0, 
    this.onTap
  }) : super(key: key);

  CYParagraph.copyWith(CYParagraph old, {
    String data,
    Key key, 
    double lineHeight, 
    double fontSize, 
    FontWeight fontWeight, 
    Color color, 
    TextOverflow overflow,
    EdgeInsetsGeometry margin, 
    EdgeInsetsGeometry padding,
    Color backgroundColor,
    double height,
    Alignment alignment,
    double width,
    TextAlign textAlign,
    int maxLines,
    bool selectable,
    List<Shadow> shadows,
    bool expandable = false,
    VoidCallback onExpanded,
    double borderRadius,
    VoidCallback onTap,
  }): 
  this.data = data ?? old.data,
  this.lineHeight = lineHeight ?? old.lineHeight,
  this.fontSize = fontSize ?? old.fontSize,
  this.fontWeight = fontWeight ?? old.fontWeight,
  this.color = color ?? old.color,
  this.overflow = overflow ?? old.overflow,
  this.margin = margin ?? old.margin,
  this.padding = padding ?? old.padding,
  this.textAlign = textAlign ?? old.textAlign,
  this.backgroundColor = backgroundColor ?? old.backgroundColor,
  this.height = height ?? old.height,
  this.alignment = alignment ?? old.alignment,
  this.width = width ?? old.width,
  this.selectable = selectable ?? old.selectable,
  this.maxLines = maxLines ?? old.maxLines,
  this.expandable = expandable ?? old.expandable,
  this.onExpanded = onExpanded ?? old.onExpanded,
  this.shadows = shadows ?? old.shadows,
  this.borderRadius = borderRadius ?? old.borderRadius,
  this.onTap = onTap ?? old.onTap;

  buildOverFlow(BuildContext context) {
    var cs = CYThemeData.of(context);
    if(expandable == true) {
      return OverFlowTextSpan(
        children: <TextSpan>[
        TextSpan(text: '\u2026'),
        expandable ? TextSpan(
          text: "    更多",
          style: TextStyle(
            color: cs.foreColorSystem.secondaryColor,
          ),
          recognizer: TapGestureRecognizer()
            ..onTap = onExpanded
        ) : TextSpan(text: '')
      ]);
    }  else {
      if(overflow == null || overflow == TextOverflow.visible) {
        return null;
      }
      return OverFlowTextSpan(children: <TextSpan>[
        TextSpan(text: '\u2026')
      ]);
    }
  }

  @override
  Widget build(BuildContext context) {
    var cs = CYThemeData.of(context);
    double textHeight = lineHeight == null ? null : lineHeight/fontSize;
    TextStyle pStyle = TextStyle(
      textBaseline: TextBaseline.ideographic, 
      color: color ?? cs.primaryForeColor, 
      fontWeight: fontWeight, 
      fontSize: fontSize, 
      height: textHeight,
      decoration: TextDecoration.none,
      shadows: shadows,
    );
    TextAlign _textAlign = textAlign ?? TextAlign.left;
    
    return Container(
      margin: margin,
      padding: padding,
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: borderRadius == null ? null : BorderRadius.circular(borderRadius)
      ),
      height: height,
      width: width,
      alignment: alignment ?? Alignment.centerLeft,
      child: ExtendedText(
        data ?? "",
        selectionEnabled: selectable,
        textAlign: _textAlign,
        style: pStyle,
        overflow: overflow,
        maxLines: maxLines,
        overFlowTextSpan: buildOverFlow(context),
        onTap: onTap,
      )
    );
  }
}